package 行为型模式.访问者;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassNameVisitorTest
 * @Description 抽象访问者（具体元素ABC。。。）
 * @Author
 * @Date2022/1/6 16:18
 **/
public class VisitorTest {
    public static void main(String[] args) {
        SetMaterial os = new SetMaterial();    //材料集对象
        Company visitor1, visitor2;    //访问者对象
        os.add(new Cuprum());
        os.add(new Paper());
        visitor1 = new ArtCompany();//艺术公司
        visitor2 = new Mint(); //造币公司
        os.accept(visitor1);

        System.out.println("=================");
        os.accept(visitor2);
    }
}
//抽象访问者:公司
interface Company {
    void create(Paper element);
    void create(Cuprum element);
}
//具体访问者：艺术公司
class ArtCompany implements Company {
    @Override
    public void create(Paper element) {
        System.out.println("讲学图");
    }
    @Override
    public void create(Cuprum element) {
        System.out.println("朱熹铜像");
    }
}
//具体访问者：造币公司
class Mint implements Company {
    @Override
    public void create(Paper element) {
        System.out.println("纸币");
    }
    @Override
    public void create(Cuprum element) {
        System.out.println("铜币");
    }
}
//抽象元素：材料
interface Material {
    void accept(Company visitor);
}
//具体元素：纸
class Paper implements Material {
    @Override
    public void accept(Company visitor) {
        visitor.create(this);
    }
}
//具体元素：铜
class Cuprum implements Material {
    @Override
    public void accept(Company visitor) {
         visitor.create(this);
    }
}
//对象结构角色:材料集
class SetMaterial {
    private List<Material> list = new ArrayList<Material>();
    public String accept(Company visitor) {
        Iterator<Material> i = list.iterator();
        String tmp = "";
        while (i.hasNext()) {
            ((Material) i.next()).accept(visitor) ;
        }
        return tmp; //返回某公司的作品集
    }
    public void add(Material element) {
        list.add(element);
    }
    public void remove(Material element) {
        list.remove(element);
    }
}
